package com.zrrd.gmall.webll.controller;

import com.zrrd.gmall.common.result.Result;
import com.zrrd.gmall.list.fegin.ListFefinClient;
import com.zrrd.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFefinClient listFefinClient;

    //商品列表搜索请求
    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model) {
        //调用fegin组件发起请求 获取搜索结果数据
        Result<Map> result = listFefinClient.list(searchParam);
        System.out.println("搜索商品的结果====>" + result.getData());

        //存入搜索结果
        model.addAllAttributes(result.getData());
        //存入拼接好的url
        model.addAttribute("urlParam", this.makeUrlParam(searchParam));
        //存入排序的map
        model.addAttribute("orderMap", this.makeOrderMap(searchParam.getOrder()));
        //存入品牌条件回显数据
        model.addAttribute("trademarkParam", this.makeTrademark(searchParam.getTrademark()));
        //存入平台条件回显数据
        model.addAttribute("propsParamList",this.makeProps(searchParam.getProps()));

        //将数据转发到指定页面
        return "/list/index";
    }

    //制作前段需要的参数拼接的url字符串
    private String makeUrlParam(SearchParam searchParam) {
        //创建字符串缓冲区
        StringBuilder builder = new StringBuilder();
        //关键字
        if (searchParam.getKeyword() != null) {
            builder.append("keyword=").append(searchParam.getKeyword());
        }
        //分类
        if (searchParam.getCategory1Id() != null) {
            builder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null) {
            builder.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null) {
            builder.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //品牌
        if (searchParam.getTrademark() != null) {
            builder.append("&trademark=").append(searchParam.getTrademark());
        }
        //平台属性
        if (searchParam.getProps() != null) {
            for (String prop : searchParam.getProps()) {
                if (prop.length() > 0) {
                    builder.append("&props=").append(prop);
                }
            }
        }
        //返回整的拼接好的url字符串
        return "list.html?" + builder.toString();
    }

    //制作前段需要的排序规则Map集合
    private Map<String, Object> makeOrderMap(String order) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }
        } else {
            map.put("type", "1");
            map.put("sort", "asc");
        }
        return map;
    }

    //制作品牌条件回显字符串
    private String makeTrademark(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            if (split != null && split.length == 2) {
                return "品牌：" + split[1];
            }
        }
        return "";
    }

    //制作平台属性条件回显集合
    private List<Map<String, String>> makeProps(String[] props) {
        List<Map<String, String>> list = new ArrayList<>();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    //创建一个Map
                    Map<String, String> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }


}
